<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      const xhr = new XMLHttpRequest();
      xhr.open("GET", "http://127.0.0.1:3000/posts");
      xhr.onload = function () {
        // 单个获取
        console.log(xhr.getResponseHeader("Content-Type"));
        console.log(xhr.getResponseHeader("Date"));
        console.log(xhr.getAllResponseHeaders());
        // 全部获取
        const arr = xhr
          .getAllResponseHeaders()
          .split("\r\n") //转一维数组
          .slice(0, -1) //删除最后一个元素
          .map((el) => {
            return el.split(": "); //转二维数组
          });
        // 二维数组转对象
        console.log(Object.fromEntries(arr));
      };
      xhr.send();
    </script>
  </body>
</html>
